package com.zzl.myblog.View.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zzl.myblog.Bean.ViewBean.ArticleBean;
import com.zzl.myblog.R;
import com.zzl.myblog.Utils.GlideImageLoader;

import java.util.List;


/**
 * 每日文章推荐适配器（top）
 * — recycleView
 * - cardView
 */

/**
 * 1. 为RecyclerView准备一个适配器：TopArticleAdater
 * 2. 指定泛型为 TopArticleAdapter.ViewHolder
 * 3. viewHolder是我们在适配器中定义的一个内部类
 */
public class TopArticleAdapter  extends RecyclerView.Adapter<TopArticleAdapter.ViewHolder> {

    //　数据源
    private List<ArticleBean> articleBeanList;

    private Context context;

    /**
     * Adapter构造函数
     * 用于将要展示的数据布置给全局变量articleBeanList
     * @param articleBeanList
     */
    public TopArticleAdapter(Context context,List<ArticleBean> articleBeanList) {
        this.context = context;
        this.articleBeanList = articleBeanList;
    }

    /**
     * onCreateViewHolder
     * - 将子布局加载进来
     * - 创建一个viewHolder实例
     * - 把加载出来的布局传入到viewHolder当中
     * - 将viewHolder实例返回
     * @param viewGroup
     * @param i
     * @return
     */
    @NonNull
    @Override
    public TopArticleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        // 子布局
        View itemView = LayoutInflater.from(viewGroup.getContext()).
                inflate(R.layout.article_card_item, viewGroup, false);

        //将子布局传入viewholder
        return new ViewHolder(itemView);

    }

    /**
     * onBindViewHolder
     * - 对RecycleView子项的数据进行复制
     * - 此函数会在每个子项被滚动到屏幕内的时候执行
     * - 通过positioni参数获取到当前的实例，并把数据设置到ViewHolder的UI控件当中即可
     * @param ViewHolder
     * @param
     */
    @Override
    public void onBindViewHolder(@NonNull TopArticleAdapter.ViewHolder ViewHolder, int position) {
        // 拿到子项对应的数据源实例
        ArticleBean articleBean=articleBeanList.get(position);
        // 填充该子项布局
        ViewHolder.author.setText(articleBean.getAuthor());
        ViewHolder.title.setText(articleBean.getTitle());
        ViewHolder.time.setText(articleBean.getTime());
        ViewHolder.kind.setText(articleBean.getKind());
        // 设置子项父布局CardView点击事件
        ViewHolder.article.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                  参数：
                    - 当前子项 View
                    - 当前位置 position
                 */
                onItemClickListener.onItemClick(ViewHolder.itemView, position);
            }
        });
    }

    /**
     * getItemCount
     * 告诉RecycleView一共有多少子项，返回数据源长度就可
     * @return
     */
    @Override
    public int getItemCount() {
        return articleBeanList.size();
    }

    /**
     * 定义一个内部类 ViewHolder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        protected CardView article;
        protected TextView author;
        protected TextView title;
        protected TextView time;
        protected TextView kind;
        /**
         * 在构造函数中传入一个View参数【这个参数通常是RecyclerView子项最外层布局】
         * 之后就可以通过FindViewById拿到布局里的实例了
         * @param itemView
         */
        public ViewHolder(View itemView) {
            super(itemView);
            article=itemView.findViewById(R.id.cv_top_article_card);
            author = itemView.findViewById(R.id.tv_top_article_author);
            title = itemView.findViewById(R.id.tv_top_article_title);
            time = itemView.findViewById(R.id.tv_top_article_time);
            kind = itemView.findViewById(R.id.tv_top_article_kind);
        }
    }

    // adapter 实例调用实现
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        // 为属性onItemClickListener赋值
        this.onItemClickListener = onItemClickListener;
    }

    private OnItemClickListener onItemClickListener;

    /**
     * 自定义监听回调接口，RecyclerView 的单击事件
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);// 参数：当前子项的子布局，位置
    }

}//adapter.class